package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.model.MyTicketModel;
import com.zw.yzk.kabu.model.QRCodeModel;
import com.zw.yzk.kabu.utils.TimeFormatUtils;
import com.zw.yzk.kabu.view.activity.TicketQrCodeActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/2/8.
 */

public class MyTicketListAdapter extends RecyclerView.Adapter<MyTicketListAdapter.Holder> {

    private List<MyTicketModel> dataList;
    private Context context;

    public MyTicketListAdapter(List<MyTicketModel> list) {
        dataList = new ArrayList<>();
        if (list != null) {
            dataList.addAll(list);
        }
    }

    public void refresh(List<MyTicketModel> list) {
        dataList.clear();
        if (list != null) {
            dataList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void add(List<MyTicketModel> list) {
        if (list != null) {
            int position = dataList.size();
            dataList.addAll(list);
            notifyItemRangeInserted(position, list.size());
        }
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_ticket_list, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        final MyTicketModel model = dataList.get(position);
        holder.title.setText(model.getTitle());
        holder.time.setText(model.getTime());
        holder.type.setText(model.getTicketType());
        holder.invalid.setVisibility(model.isInvalid() ? View.VISIBLE : View.GONE);

        Glide.with(context)
                .load(model.getImage())
                .error(R.drawable.im_default)
                .into(holder.imageView);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QRCodeModel qrCodeModel = new QRCodeModel();
                qrCodeModel.setTitle(model.getTitle());
                qrCodeModel.setCount(model.getCount());
                qrCodeModel.setTotalPrice(model.getTotalPrice());
                qrCodeModel.setSerialNumber(model.getSerialNumber());
                qrCodeModel.setCheckCode(model.getCheckCode());
                qrCodeModel.setInvalid(model.isInvalid());
                String time;
                try {
                    time = TimeFormatUtils.stringDateFormat(model.getStartTime(), "yyyy-MM-dd HH:mm:ss", "yyyy年MM月dd日 HH:mm") + " - "
                            + TimeFormatUtils.stringDateFormat(model.getStartTime(), "yyyy-MM-dd HH:mm:ss", "MM月dd日 HH:mm");
                } catch (Exception e) {
                    time = model.getStartTime() + "-" + model.getEndTime();
                }
                qrCodeModel.setTime(time);

                Intent intent = new Intent(context, TicketQrCodeActivity.class);
                intent.putExtra(ActivityConstant.QR_CODE_DATA, qrCodeModel);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {

        @Bind(R.id.exhibition_image)
        ImageView imageView;
        @Bind(R.id.title)
        TextView title;
        @Bind(R.id.time)
        TextView time;
        @Bind(R.id.type)
        TextView type;
        @Bind(R.id.invalid)
        ImageView invalid;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
